//
//  TKMyMainController.m
//  Ticket
//
//  Created by 小禹宝宝 on 2017/10/9.
//  Copyright © 2017年 cy55. All rights reserved.
//

#import "TKMyMainController.h"
#import "TKMineTableHeaderView.h"
#import "TKMineTopTableViewCell.h"
#import "TKMineMiddleTableViewCell.h"
#import "TKMineBottomTableViewCell.h"
#import "AuctionManagerCell.h"
#import "TKPersonalInfoViewController.h"
//#import "TKAuctioneerManagementController.h"
#import "TKAuctioneerManagementListController.h"
#import "TKMainGoodsDetailSectionView.h"
#import "TKMyOrderViewController.h"

@interface TKMyMainController()
<UITableViewDelegate,
UITableViewDataSource,
UIGestureRecognizerDelegate>
@property (nonatomic,strong) UITableView * tableView;
@property (nonatomic, strong) TKMineTableHeaderView *topView;

//@property (nonatomic, strong) NSMutableArray<BLMessageInfo *> *messages;


@property (nonatomic, strong) NSTimer *autoRefreshMsgTimer;
@end
@implementation TKMyMainController

-(void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"个人中心";
    [self creatTableview];
    [self creaTableHeaderView];
//    if ([TKAccountManager isLogin]) {
//        [[BLMessageManager sharedInstance] readMessages];
//    }
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(udDLoginSuccess:) name:kUDLoginSuccess object:nil];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self refeshTopView];
    [self.tableView reloadData];
//    [self updateTabbarbadge];
}

- (void)creatTableview {
    _tableView = ({
        UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        //        tableView.backgroundColor = [UIColor redColor];
        tableView.delegate = self;
        tableView.dataSource = self;
        //        tableView.scrollEnabled = NO;
        [self.view addSubview:tableView];
        [tableView registerNib:[UINib nibWithNibName:@"TKMineTopTableViewCell" bundle:nil] forCellReuseIdentifier:@"TKMineTopTableViewCell"];
        [tableView registerNib:[UINib nibWithNibName:@"TKMineMiddleTableViewCell" bundle:nil] forCellReuseIdentifier:@"TKMineMiddleTableViewCell"];
        [tableView registerNib:[UINib nibWithNibName:@"TKMineBottomTableViewCell" bundle:nil] forCellReuseIdentifier:@"TKMineBottomTableViewCell"];
        [tableView registerNib:[UINib nibWithNibName:@"AuctionManagerCell" bundle:nil] forCellReuseIdentifier:@"AuctionManagerCell"];
        
        tableView.translatesAutoresizingMaskIntoConstraints = NO;
        [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.view);
            make.top.equalTo(self.view);
            make.size.height.equalTo(@(mScreenHeight));
        }];

        tableView;
    });
}

- (void)creaTableHeaderView {
    self.topView = [[[NSBundle mainBundle] loadNibNamed:@"TKMineTableHeaderView" owner:nil options:nil] firstObject];
    self.tableView.tableHeaderView = self.topView;
    
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.tableView);
        make.height.mas_equalTo(120);
    }];
    UITapGestureRecognizer * headerTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapheaderView:)];
    headerTap.numberOfTouchesRequired = 1; //手指数
    headerTap.numberOfTapsRequired = 1; //tap次数
    headerTap.delegate= self;
    [self.topView addGestureRecognizer:headerTap];
}





- (void)udDLoginSuccess:(NSNotification *)notification {
//    [self beginFetchNewMessagesLoop];
    [self refeshTopView];
}

#pragma tableView--delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 51;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }
    if (section == 1) {
        return 2;
    }
    return 0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        AuctionManagerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AuctionManagerCell" forIndexPath:indexPath];
        cell.titleLabel.text = @"订单列表";
        return cell;
    }
    if (indexPath.section == 1) {
        AuctionManagerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AuctionManagerCell" forIndexPath:indexPath];

        switch (indexPath.row) {
            case 0:
            {
                cell.titleLabel.text = @"拍品管理";
            }
                break;
            case 1:
            {
                cell.titleLabel.text = @"订单列表";
            }
                break;
                
            default:
                break;
        }
        return cell;
    }
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        //进入买家订单列表
        TKMyOrderViewController *vc = [TKMyOrderViewController new];
        vc.buy_user_id = [TKAccountManager sharedManager].accountInfo.result.info.user_id;
        [KSNavi push:vc];
    }
    
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            TKAuctioneerManagementListController *mVC = [TKAuctioneerManagementListController new];
            [KSNavi push:mVC];
        }
        if (indexPath.row == 1) {
            //进入卖家订单列表
            TKMyOrderViewController *orderVC = [TKMyOrderViewController new];
            orderVC.sell_user_id = [TKAccountManager sharedManager].accountInfo.result.info.user_id;
            [KSNavi push:orderVC];
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 61;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    TKMainGoodsDetailSectionView *headerSectionView = [[[NSBundle mainBundle] loadNibNamed:@"TKMainGoodsDetailSectionView" owner:nil options:nil] firstObject];
    switch (section) {
        case 0:
            headerSectionView.titleLabel.text = @"买家中心";
            break;
        case 1:
            headerSectionView.titleLabel.text = @"卖家中心";
            break;

        default:
            break;
    }
    return headerSectionView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 0) {
        return 10;
    }
    return 0.5f;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [BLColorConfig backgroundColor];
    return view;
}
#pragma refeshTopViewData
- (void)refeshTopView {
    if(![SystemHelper checkAndShowLoginViewControllerIfNotLogin]) {
        self.topView.nickNameLabel.text = @"未登录";
        self.topView.iconImageView.image = [UIImage imageNamed:@"iconp_laceholder"];
        self.tableView.height = 410;
        
    } else {
        self.topView.model = [TKAccountManager sharedManager].accountInfo.result.info;
    }
}


#pragma mark -topView 跳转个人设置页面
- (void)tapheaderView: (UITapGestureRecognizer *)gesture
{
    if ([SystemHelper checkAndShowLoginViewControllerIfNotLogin]) {
        TKPersonalInfoViewController *pVC = [[TKPersonalInfoViewController alloc] init];
        pVC.personalInfo = [TKAccountManager sharedManager].accountInfo.result.info;
        @weakify(pVC)
        @weakify(self)
        pVC.submitBlock = ^(id a) {
            @strongify(pVC)
            @strongify(self)
            [TKAccountManager requestUserInfoCompletion:^{
                if ([TKAccountManager sharedManager].accountInfo.result.info) {
                    pVC.personalInfo = [TKAccountManager sharedManager].accountInfo.result.info;
                    [self refeshTopView];
                }
            }];

        };
        [self.navigationController pushViewController:pVC animated:YES];
    }

}

@end
